<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深拷贝</title>
</head>

<body>

</body>
<script>
    const objList = {
        name: '李涛',
        age: 22,
        sex: 1,
        arr: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        obj: {
            name: '李涛',
            age: 22,
            sex: 1,
            arr: [1, 2, 3, 4, 5, 6, 7, 8, 9],
        }
    }

    console.log(objList);

    function deepClone(objList) {
        const targetObj = objList.constructor === Array ? [] : {}
        for (const key in objList) {
            if(objList.hasOwnProperty(key)){
                // 引用数据类型
                if(objList[key] && typeof objList[key] === 'object'){
                    targetObj[key] = objList[key].constructor === Array ? [] : {}
                    targetObj[key] = deepClone(objList[key])
                }else{
                    // 基本数据类型
                    targetObj[key] = objList[key]
                }
            }
        }
        return targetObj
    }

    const newObjList = deepClone(objList)
    newObjList.name = '喜气洋洋'
    console.log(newObjList)

</script>

</html>